//
//  RegistView.m
//  HeWeiZhiNeng
//
//  Created by 林建 on 17/12/3.
//  Copyright © 2017年 YanCeyMichael.github.io. All rights reserved.
//

#import "RegistView.h"

@implementation RegistView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = Color.back;
        //手机·邮箱
        self.phoneView = [[TextField_ImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 60) left:15 right:-15 imageLeft:30];
        [self addSubview:self.phoneView];
        self.phoneView.OutPutTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"输入手机号/邮箱" attributes:@{NSForegroundColorAttributeName:Color.stub}];
        [self.phoneView.OutPutTF setValue:Common_Font(15) forKeyPath:@"_placeholderLabel.font"];
        self.phoneView.ContextView.image = [UIImage imageNamed:@"zhanghao_shoji_youxiang"];
        //验证码
        self.codeView = [[TextField_ImageView alloc]initWithFrame:CGRectMake(0, 60, kScreenW, 60) left:15 right:-15 imageLeft:30];
        [self addSubview:self.codeView];
        self.codeView.OutPutTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"输入验证码" attributes:@{NSForegroundColorAttributeName:Color.stub}];
        [self.codeView.OutPutTF setValue:Common_Font(15) forKeyPath:@"_placeholderLabel.font"];
        self.codeView.ContextView.image = [UIImage imageNamed:@"zhanghao_yanzhengma"];
        //获取验证码
        self.timeLabel = [[UILabel alloc]init];
        self.timeLabel.text = @"获取验证码";
        self.timeLabel.layer.cornerRadius = 5;
        self.timeLabel.layer.masksToBounds = YES;
        self.timeLabel.userInteractionEnabled = YES;
        self.timeLabel.textAlignment = NSTextAlignmentCenter;
        self.timeLabel.font = Common_Font(15);
        self.timeLabel.textColor = Color.fullBtnTitle;
        self.timeLabel.backgroundColor = Color.selected_blue;
        [self addSubview:self.timeLabel];
        //密码
        self.PWView = [[TextField_ImageView alloc]initWithFrame:CGRectMake(0, 120, kScreenW, 60) left:15 right:-15 imageLeft:30];
        [self addSubview:self.PWView];
        self.PWView.OutPutTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"输入密码" attributes:@{NSForegroundColorAttributeName:Color.stub}];
        [self.PWView.OutPutTF setValue:Common_Font(15) forKeyPath:@"_placeholderLabel.font"];
        self.PWView.ContextView.image = [UIImage imageNamed:@"zhanghao_mima"];
        //确认密码
        self.S_PWView = [[TextField_ImageView alloc]initWithFrame:CGRectMake(0, 180, kScreenW, 60) left:15 right:-15 imageLeft:30];
        [self addSubview:self.S_PWView];
        self.S_PWView.OutPutTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"确认密码" attributes:@{NSForegroundColorAttributeName:Color.stub}];
        [self.S_PWView.OutPutTF setValue:Common_Font(15) forKeyPath:@"_placeholderLabel.font"];
        self.S_PWView.ContextView.image = [UIImage imageNamed:@"zhuce_querenmima"];
        //同意框
        self.agreeView = [UIImageView new];
        self.agreeView.userInteractionEnabled = YES;
        self.agreeView.image = [UIImage imageNamed:@"zhuce_tonyi_gou"];
        [self addSubview:self.agreeView];
        //同意标签
        self.agreeLabel = [UILabel new];
        [self addSubview:self.agreeLabel];
        [self CreatNewLabel:self.agreeLabel title:@"同意" color:Color.stub font:Common_Font(12)];
        //协议标签
        self.delegateLabel = [UILabel new];
        [self addSubview:self.delegateLabel];
        [self CreatNewLabel:self.delegateLabel title:@"使用协议" color:Color.selected_blue font:Common_Font(12)];
        //注册按钮
        self.registBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.registBtn setTitle:@"完成" forState:UIControlStateNormal];
        [self.registBtn setTitleColor:Color.fullBtnTitle forState:UIControlStateNormal];
        [self.registBtn addTarget:self action:@selector(regist) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.registBtn];
        self.registBtn.titleLabel.font = Common_Font(15);
        self.registBtn.backgroundColor = Color.theme;
        self.registBtn.layer.masksToBounds = YES;
        self.registBtn.layer.cornerRadius = 5;
        
        //分割线
        UIView *lineV1 = [[UIView alloc]init];
        lineV1.backgroundColor = Color.line;
        [self addSubview:lineV1];
        UIView *lineV2 = [[UIView alloc]init];
        lineV2.backgroundColor = Color.line;
        [self addSubview:lineV2];
        UIView *lineV3 = [[UIView alloc]init];
        lineV3.backgroundColor = Color.line;
        [self addSubview:lineV3];
        [lineV1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.phoneView.mas_bottom);
            make.left.mas_equalTo(self.mas_left).offset(15);
            make.height.mas_equalTo(1);
            make.right.mas_equalTo(self.mas_right).offset(-15);
        }];
        [lineV2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.codeView.mas_bottom);
            make.left.mas_equalTo(self.mas_left).offset(15);
            make.height.mas_equalTo(1);
            make.right.mas_equalTo(self.mas_right).offset(-15);
        }];
        [lineV3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.PWView.mas_bottom);
            make.left.mas_equalTo(self.mas_left).offset(15);
            make.height.mas_equalTo(1);
            make.right.mas_equalTo(self.mas_right).offset(-15);
        }];
        
        UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(agree)];
        [self.agreeView addGestureRecognizer:tapGr];
    }
    return self;
}

-(void)CreatNewLabel:(UILabel *)label title:(NSString *)title color:(UIColor *)color font :(UIFont *)font{
    label.text = title;
    label.userInteractionEnabled = YES;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = color;
    label.font = font;
}

-(void)layoutSubviews{
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.codeView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(90, 30));
        make.right.mas_equalTo(self.mas_right).offset(-15);
    }];
    [self.agreeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.S_PWView.mas_bottom).offset(10);
        make.left.mas_equalTo(self.mas_left).offset(15);
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    [self.agreeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.agreeView.mas_right).offset(30);
        make.centerY.mas_equalTo(self.agreeView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake([self getTextWidth:self.agreeLabel.text withHeight:14 font:Common_Font(12)], 14));
    }];
    [self.delegateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.agreeLabel.mas_right).offset(15);
        make.centerY.mas_equalTo(self.agreeView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake([self getTextWidth:self.delegateLabel.text withHeight:14 font:Common_Font(12)], 14));
    }];
    [self.registBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.agreeView.mas_bottom).offset(30);
        make.left.mas_equalTo(self.mas_left).offset(15);
        make.right.mas_equalTo(self.mas_right).offset(-15);
        make.height.mas_equalTo(44);
    }];
}

- (void)regist{
    if ([self.delegate respondsToSelector:@selector(userChoose:)]) {
        [self.delegate userChoose:109];
    }
}

- (void)agree{
    if ([self.delegate respondsToSelector:@selector(userChoose:)]) {
        [self.delegate userChoose:110];
    }
}

-(CGFloat)getTextWidth:(NSString *)text withHeight:(CGFloat)height font:(UIFont *)font{
    CGSize newSize = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
    
    return newSize.width;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
